// router模块
import Vue from 'vue'
import VueRouter from 'vue-router'

import Home from "@/components/Home.vue";
import Movie from "@/components/Movie.vue";
import About from "@/components/About.vue";
import Main from "@/components/Main.vue";
import Login from "@/components/Login.vue";

import tab1 from "@/components/tab/tab1.vue";
import tab2 from "@/components/tab/tab2.vue";


import MovieInfo from "@/components/MovieInfo.vue";

Vue.use(VueRouter)

const router = new VueRouter({
    // 定义hash地址和组件之间的对应关系
    routes: [
        // 强制跳转
        { path: '/', redirect: '/home' },
        { path: '/home', component: Home },
        {
            path: '/movie',
            component: Movie,
            children: [{ path: 'movieInfo/:movieId', component: MovieInfo, props: true }]
        },
        {
            path: '/about',
            component: About,
            children: [
                // 默认子陆游
                { path: '', component: tab1 },
                { path: 'tab2', component: tab2 },
            ]
        },
        { path: '/main', component: Main },
        { path: '/login', component: Login }
    ]
})


/**
 * 鉴权
 * 只要发生了页面路由跳转，就触发
 * to from next
 * 
 **/
router.beforeEach(function(to, from, next) {

    // to 要去哪
    // console.log(to)

    // from 从哪来
    // console.log(from)

    // 放行
    // next()

    if (to.path === '/main') {
        const token = localStorage.getItem('token')
        console.log(token)
        if (token) {
            next()
        } else {
            next('/login')
        }
    } else {
        next()
    }

})

export default router